import api from "../../utils/api";
import { throttle } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchTxt: "",
    vaccineProductList: [],
    vaccineProductLen: 0,
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options: ", options);
    const { searchTxt } = options;
    if (searchTxt) {
      this.getSearchResult(searchTxt);
      this.setData({
        searchTxt,
      });
    }
  },

  onBindinput(e) {
    this.setData({
      searchTxt: e.detail || "",
    });
  },

  onBindconfirm() {
    this.getSearchResult(this.data.searchTxt);
  },

  /**
   * @description: 获取数据
   * @param {*} search
   */
  getSearchResult(search) {
    this.setData({
      loading: true,
    });
    api.searchResult({
      data: {
        search,
      },
      success: (res) => {
        const { data, code } = res || {};
        if (+code === 0) {
          this.setData({
            loading: false,
            vaccineProductList: data.vaccineProductList,
            vaccineProductLen: (data.vaccineProductList || []).length || 0,
          });
        } else {
          this.setData({
            loading: false,
            vaccineProductLen: 0,
          });
        }
      },
      fail: (err) => {
        this.setData({
          loading: false,
          vaccineProductLen: 0,
        });
      },
    });
  },
  jumpDetail(e) {
    const { id } = e.currentTarget.dataset;
    throttle(() => {
      wx.navigateTo({
        url: `../detail/detail?productMirrorId=${id}&type=1`,
      });
    });
  },
});
